/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.ast;

import alloy.type.UnionType;

/**
   a base implementation for expressions.
*/

public abstract class TypedExpr extends TreeNode implements Expr {

    /** the {@link UnionType} of this expression */
    private UnionType _type;
    
    /** paren status of this */
    private boolean _parens;

    public TypedExpr(Location location_) {
        super(location_);
    }
    
    public TypedExpr(Location location_, Node n1_) {
        super(location_, n1_);
    }


    public TypedExpr(Node n1_) {
        super(Location.UNKNOWN, n1_);
    }


    
    public TypedExpr(Location location_, Node n1_, Node n2_) {
        super(location_, n1_, n2_);
    }


    public TypedExpr(Node n1_, Node n2_) {
        super(Location.UNKNOWN, n1_, n2_);
    }


    
    public TypedExpr(Location location_, Node n1_, Node n2_, Node n3_) {
        super(location_, n1_, n2_, n3_);
    }


    public TypedExpr(Node n1_, Node n2_, Node n3_) {
        super(Location.UNKNOWN, n1_, n2_, n3_);
    }



    /**
     * get the type of this
     * @return the type of this
     */
    public UnionType getType() {
        return _type;
    }
    
    /**
     * set the type of this
     * @param type_ new type for this
     */
    public final void setType(UnionType type_) {
        _type = type_;
	//System.out.println("Set type of " + nodeString() + " to " + type_);
    }

    void setDescr() {
	if (_type != null) {
            descr += "type " + _type.toString();
        }
    }
    
    public boolean hasParens() {
	return _parens;
    }
	
    public void setParens(boolean parens_) {
	_parens = parens_;
    }
	
    public Object copy() {
        TypedExpr exprCopy = (TypedExpr)super.copy();
        exprCopy.setType(_type);
        return exprCopy;
    }

    public BinaryExpr join(Expr other_) {
        BinaryExpr ret = new BinaryExpr(this, BinaryExprOp.JOIN_DOT, other_);
        if (getType() != null && other_.getType() != null) {
	    ret.setType(getType().join(other_.getType()));            
        }
        return ret;
    }

    public BinaryExpr union(Expr other_) {
        BinaryExpr ret = new BinaryExpr(this, BinaryExprOp.UNION, other_);
        if (getType() != null && other_.getType() != null) {
            ret.setType(getType().union(other_.getType()));
        }
        return ret;
    }

    public BinaryExpr diff(Expr other_) {
        BinaryExpr ret = new BinaryExpr(this, BinaryExprOp.DIFF, other_);
        if (getType() != null && other_.getType() != null) {
            ret.setType(getType());
        }
        return ret;
    }

    public BinaryExpr intersect(Expr other_) {
        BinaryExpr ret = new BinaryExpr(this, BinaryExprOp.INTERSECT, other_);
        if (getType() != null && other_.getType() != null) {
            ret.setType(getType().intersect(other_.getType()));
        }
        return ret;
    }

    public BinaryExpr override(Expr other_) {
        BinaryExpr ret = new BinaryExpr(this, BinaryExprOp.RELATIONALOVERRIDE, other_);
        if (getType() != null && other_.getType() != null) {
            ret.setType(getType().union(other_.getType()));
        }
        return ret;
    }

    public BinaryExpr product(Expr other_) {
        BinaryExpr ret = new BinaryExpr(this, BinaryExprOp.RELATION, other_);
        if (getType() != null && other_.getType() != null) {
            ret.setType(getType().product(other_.getType()));
        }
        return ret;
    }

    public ElemFormula in(Expr other_) {
        return new ElemFormula(this, CompOp.SUBSETIN, other_);
    }

    public ElemFormula eq(Expr other_) {
        return new ElemFormula(this, CompOp.EQUALS, other_);
    }
        
    /**
     * accept an AST visitor
     * @param visitor_ the visitor
     */
    protected void acceptVisitor(ASTVisitor visitor_) {
        visitor_.visit(this);
    }

    /**
     * accept a returning AST visitor
     * @param visitor_ the visitor
     * @return the Object returned by the visitor
     */
    protected Object acceptReturnVisitor(ASTReturnVisitor visitor_) {
        return visitor_.visit(this);
    }

}





